package com.css.fxfzysx.modules.lifeline.utils;

import com.css.fxfzysx.modules.lifeline.constants.SystemBasicDataEnum;
import com.css.fxfzysx.modules.lifeline.repository.LifelineRepository;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.sun.istack.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Configuration
public class VersionCodeUtils {

    @Autowired
    private LifelineRepository lifelineRepository;

    public synchronized String getVersionCode(@NotNull Integer type) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String time = LocalDate.now().format(formatter);
        String versionCode = "";
        String code = lifelineRepository.getCode(type);
        if(PlatformObjectUtils.isNotEmpty(code)){
            String[] split = code.split("-");
            versionCode = split[2];
            StringBuilder stringBuilder = new StringBuilder();
            String t = String.valueOf(Integer.parseInt(versionCode) + 1);
            for (int i = t.length(); i < 4; i++) {
                stringBuilder.append("0");
            }
            versionCode = stringBuilder.append(t).toString();
        }else {
            versionCode = "0001";
        }
        versionCode = SystemBasicDataEnum.getByCode(type)+"-"+time+"-"+versionCode;
        return versionCode;
    }
}
